import axios from 'axios'
import qs from 'qs'
import {
  getToken
} from './token.js'

/* axios.post('/api/regist', {
    'user_phone': context.state.user_phone,
    'user_password': context.state.user_password,
    'user_name': context.state.user_name
  }).then(function(response) {
    console.log(response);
  })
  .catch(function(error) {
    console.log(error);
  });

axios.post('/api/login', {
    'username': context.state.user_phone,
    'password': context.state.user_password
  }).then(function(response) {
    console.log(response);
  })
  .catch(function(error) {
    console.log(error);
  });
 */
const instance = axios.create({
  baseURL: '/api/',
  timeout: 5000,
  transformRequest: function(data, headers) {
    return qs.stringify(data);
  },
});

//请求拦截
instance.interceptors.request.use(function(config) {
  //请求头中添加令牌
  config.headers.Authorization = getToken();
  return config;
});

export const get = function(url, params) {
  return instance({
    method: 'GET',
    url: url,
    params: params
  })
}

export const post = function(url, params) {
  return instance({
    method: 'POST',
    url: url,
    data: params
  })
}

export const put = function(url, params) {
  return instance({
    method: 'PUT',
    url: url,
    data: params
  })
}

export const del = (url, params) => instance({
  method: 'DELETE',
  url: url,
  data: params
})
